package com.zhufei.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import com.zhufei.entity.Company;
import com.zhufei.entity.HomePageImage;

public class CompanyDao extends DBCon{
	private static String TABLE = "zhufei_company";
	private static String ID = "ID";
	private static String COMPANY_NAME = "company_name";
	private static String PERSON = "person_in_charge";
	private static String PHONE = "telephone";
	private static String MAIL = "mail";
	private static String REMARK = "remark";
	
	/**
	 * 插入一个公司
	 * @param company_name 公司名
	 * @param person 负责人
	 * @param phone 电话
	 * @param mail 电子邮件
	 * @param remark 备注
	 * @return
	 */
	public static boolean insertCompany(String company_name, String person, String phone, String mail, String remark){
		DBCon con = new DBCon();
		try {
			String id = GlobalsDao.getUUID();
			String dt = GlobalsDao.getCurrentDateTime();
			Statement statement = con.getStatement();
			String sql = "insert into " + TABLE + " ("+ID+","+COMPANY_NAME+","+PERSON+","+PHONE+","+MAIL+","+REMARK+") values ('"+id+"','"+company_name+"','"+person+"','"+phone+"','"+mail+"','"+remark+"')";
			statement.execute(sql);
			return true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			con.closeDB();
		}
		return false;
	}
	
	/**
	 * 获取数据库中公司数量
	 * @return
	 */
	public static int getCountOfCompany(){
		DBCon con = new DBCon();
		try {
			Statement statement = con.getStatement();
			String sql = "select count(*) as count from "+TABLE;
			ResultSet rs = statement.executeQuery(sql);
			if(rs.next()){
				int count = rs.getInt("count");
				rs.close();
				return count;
			}
			else {
				rs.close();
				return 0;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			con.closeDB();
		} 
		return 0;
	}
	
	/**
	 * 获取一定数量的公司
	 * @param   从第index条开始获取（第一条下标是0）
	 * @param   获取新闻的数量，输入-1则获取数据库中所有公司
	 * @return  新闻对象的ArrayList
	 */
	public static ArrayList<Company> getImages(int index, int number){
		ArrayList<Company> companyList = new ArrayList<Company>();
		DBCon con = new DBCon();
		try {
			Statement statement = con.getStatement();
			String sql = null;
			if(number != -1){
				sql = "select * from "+ TABLE +" limit "+index+","+number;
			}else{
				sql = "select * from "+ TABLE;
			}
			
			ResultSet rs = statement.executeQuery(sql);
			while(rs.next()){
				Company c = new Company();
				c.setID(rs.getString(ID));
				c.setCompanyName(rs.getString(COMPANY_NAME));
				c.setPersonInCharge(rs.getString(PERSON));
				c.setTelephone(rs.getString(PHONE));
				c.setMail(rs.getString(MAIL));
				c.setRemark(rs.getString(REMARK));
				companyList.add(c);
			}
			rs.close();
			return companyList;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			con.closeDB();
		}
		return null;
	}
}
